/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.execute.service.impl;

import com.icinfo.cloud.common.security.util.UserUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.evideinv.service.EvidenceInventoryService;
import com.icinfo.cloud.provider.punish.execute.dto.ExecuteCluHandoutRecordDto;
import com.icinfo.cloud.provider.punish.execute.dto.ExecuteEventSaveDto;
import com.icinfo.cloud.provider.punish.execute.dto.ExecuteHandlePersonRecordDto;
import com.icinfo.cloud.provider.punish.execute.enums.ExecuteStateEnum;
import com.icinfo.cloud.provider.punish.execute.enums.ExecuteStatusEnum;
import com.icinfo.cloud.provider.punish.execute.mapper.ExecuteCluHandoutRecordMapper;
import com.icinfo.cloud.provider.punish.execute.mapper.ExecuteFlowRecordMapper;
import com.icinfo.cloud.provider.punish.execute.mapper.ExecuteMainInfoMapper;
import com.icinfo.cloud.provider.punish.execute.model.ExecuteCluHandoutRecord;
import com.icinfo.cloud.provider.punish.execute.model.ExecuteFlowRecord;
import com.icinfo.cloud.provider.punish.execute.model.ExecuteMainInfo;
import com.icinfo.cloud.provider.punish.execute.service.IExecuteCluHandoutRecordService;
import com.icinfo.cloud.provider.punish.execute.service.IExecuteHandlePersonRecordService;
import com.icinfo.cloud.xzcf.provider.member.userinfo.dto.UserDTO;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 描述: 裁执分离指派记录表 execute_clu_handout_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年08月26日
 */
@Service
public class ExecuteCluHandoutRecordServiceImpl extends MyBatisServiceSupport implements IExecuteCluHandoutRecordService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(ExecuteCluHandoutRecordServiceImpl.class);

    @Autowired
    private ExecuteCluHandoutRecordMapper executeCluHandoutRecordMapper;

    @Autowired
    private ExecuteMainInfoMapper executeMainInfoMapper;

    @Autowired
    private ExecuteFlowRecordMapper executeFlowRecordMapper;

    @Autowired
    private IExecuteHandlePersonRecordService executeHandlePersonRecordService;

    @Resource
    private EvidenceInventoryService evidenceInventoryService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveExecuteAppoint(ExecuteCluHandoutRecordDto executeCluHandoutRecordDto) {
        UserDTO userInfo = UserUtil.getUserInfo();
        ExecuteCluHandoutRecord executeCluHandoutRecord = new ExecuteCluHandoutRecord();
        BeanUtils.copyProperties(executeCluHandoutRecordDto, executeCluHandoutRecord);
        executeCluHandoutRecord.setId(StringUtils.uuid());
        executeCluHandoutRecord.setCluAppointUserOrgCode(userInfo.getOrgCode());
        executeCluHandoutRecord.setCluAppointUserOrgName(userInfo.getOrgName());
        executeCluHandoutRecord.setCluAppointUserName(userInfo.getRealName());
        executeCluHandoutRecord.setCluAppointUserId(userInfo.getUserId().toString());
        executeCluHandoutRecord.setCluAssignTime(new Date());
        executeCluHandoutRecordMapper.insertSelective(executeCluHandoutRecord);

        // 更新裁执分离主表信息
        this.updateExecuteMainInfo(executeCluHandoutRecordDto);

        // 新增裁执分离流程记录
        this.saveExecuteFlowRecord(executeCluHandoutRecordDto, userInfo);

        // 新增裁执分离办案人员记录
        this.saveExecuteHandlePersonRecord(executeCluHandoutRecordDto, userInfo);

        // 新增裁执分离事件信息
        this.saveExecuteEvent(executeCluHandoutRecordDto);
    }

    @Override
    public ExecuteCluHandoutRecord selectByExecuteId(String executeId) {
        Example example = new Example(ExecuteCluHandoutRecord.class);
        example.createCriteria().andEqualTo("executeId", executeId);
        List<ExecuteCluHandoutRecord> executeCluHandoutRecords = executeCluHandoutRecordMapper.selectByExample(example);
        if(executeCluHandoutRecords.size()>0){
            return executeCluHandoutRecords.get(0);
        }
        return null;
    }

    /**
     * 描述: 更新裁执分离主表信息
     *
     * @param executeCluHandoutRecordDto
     * @return void
     * @author zhanghongqiang
     * @Date 2022/8/29
     */
    private void updateExecuteMainInfo(ExecuteCluHandoutRecordDto executeCluHandoutRecordDto) {
        ExecuteMainInfo executeMainInfo = new ExecuteMainInfo();
        executeMainInfo.setExecuteState(ExecuteStateEnum.NOTICE.getCode());
        // 执行中
        executeMainInfo.setCaseStatus(ExecuteStatusEnum.HANDLING_EXECUTE.getIndex());
        executeMainInfo.setUpdateTime(LocalDateTime.now());
        executeMainInfo.setHandlePersonMainId(executeCluHandoutRecordDto.getCluAppointedMainPersonId());
        executeMainInfo.setHandlePersonMainName(executeCluHandoutRecordDto.getCluAppointedMainPersonName());
        executeMainInfo.setHandlePersonAssistId(executeCluHandoutRecordDto.getCluAppointedAssistPersonId());
        executeMainInfo.setHandlePersonAssistName(executeCluHandoutRecordDto.getCluAppointedAssistPersonName());
        executeMainInfo.setLineCode(executeCluHandoutRecordDto.getLineCode());
        executeMainInfo.setLineName(executeCluHandoutRecordDto.getLineName());
        executeMainInfo.setHandleOrgName(executeCluHandoutRecordDto.getPersonOrgName());
        executeMainInfo.setHandleOrgCode(UserUtil.getUserInfo().getOrgCode());
        /*executeMainInfo.setHandleDeptName(executeCluHandoutRecordDto.getCluAppointedMainPersonDeptCode());
        executeMainInfo.setHandleDeptName(executeCluHandoutRecordDto.getCluAppointedMainPersonDeptName());*/
        Example executeMainInfoExample = new Example(ExecuteMainInfo.class);
        executeMainInfoExample.createCriteria().andEqualTo("executeId", executeCluHandoutRecordDto.getExecuteId());
        executeMainInfoMapper.updateByExampleSelective(executeMainInfo, executeMainInfoExample);
    }

    /**
     * 描述: 保存指派流程日志
     *
     * @param executeCluHandoutRecordDto
     * @param userInfo
     * @return int
     * @author zhanghongqiang
     * @Date 2022/8/29
     */
    public int saveExecuteFlowRecord(ExecuteCluHandoutRecordDto executeCluHandoutRecordDto, UserDTO userInfo) {
        ExecuteFlowRecord executeFlowRecord = new ExecuteFlowRecord();
        String executeId = executeCluHandoutRecordDto.getExecuteId();
        StringBuffer flowStageContent = new StringBuffer("");
        flowStageContent.append("指派人:" + userInfo.getRealName() + "(" + userInfo.getOrgName() + "/" + userInfo.getDeptName() + ");");
        flowStageContent.append("被指派对象:");
        flowStageContent.append(executeCluHandoutRecordDto.getCluAppointedMainPersonName() + "(" + executeCluHandoutRecordDto.getPersonOrgName() + "/" + executeCluHandoutRecordDto.getCluAppointedMainPersonDeptName() + ");");
        flowStageContent.append(executeCluHandoutRecordDto.getCluAppointedAssistPersonName() + "(" + executeCluHandoutRecordDto.getPersonOrgName() + "/" + executeCluHandoutRecordDto.getCluAppointedAssistPersonDeptName() + ");");
        flowStageContent.append("指派说明:" + executeCluHandoutRecordDto.getCluAppointDesc());
        executeFlowRecord.setFlowStageContent(flowStageContent.toString());
        executeFlowRecord.setFlowEndTime(new Date());
        Example example =new Example(ExecuteFlowRecord.class);
        example.createCriteria().andEqualTo("executeId",executeId)
                .andEqualTo("flowStageCode",ExecuteStateEnum.ACCEPT_CASE.getCode());
        executeFlowRecordMapper.updateByExampleSelective(executeFlowRecord,example);
        ExecuteFlowRecord executeFlowRecordNotice = new ExecuteFlowRecord();
        executeFlowRecordNotice.setId(StringUtils.uuid());
        executeFlowRecordNotice.setExecuteId(executeId);
        executeFlowRecordNotice.setFlowStartTime(new Date());
        executeFlowRecordNotice.setFlowStageType(ExecuteStatusEnum.HANDLING_EXECUTE.getIndex());
        executeFlowRecordNotice.setFlowStageName(ExecuteStateEnum.NOTICE.getDes());
        executeFlowRecordNotice.setFlowStageCode(ExecuteStateEnum.NOTICE.getCode());
        int num = executeFlowRecordMapper.insert(executeFlowRecordNotice);
        return num;
    }

    /**
     * 描述: 新增裁执分离办案人员记录
     *
     * @param executeCluHandoutRecordDto
     * @param userInfo
     * @return void
     * @author zhanghongqiang
     * @Date 2022/8/29
     */
    private void saveExecuteHandlePersonRecord(ExecuteCluHandoutRecordDto executeCluHandoutRecordDto, UserDTO userInfo) {
        ExecuteHandlePersonRecordDto dto = new ExecuteHandlePersonRecordDto();
        dto.setExecuteId(executeCluHandoutRecordDto.getExecuteId());
        dto.setHandlePersonName(executeCluHandoutRecordDto.getCluAppointedMainPersonName());
        dto.setHandlePersonType(Constant.STRING_1);
        dto.setHandlePersonId(executeCluHandoutRecordDto.getCluAppointedMainPersonId());
        dto.setHandleOrgName(executeCluHandoutRecordDto.getPersonOrgName());
        dto.setHandleOrgCode(userInfo.getOrgCode());
        dto.setHandleDeptName(executeCluHandoutRecordDto.getCluAppointedMainPersonDeptName());
        dto.setHandleDeptCode(executeCluHandoutRecordDto.getCluAppointedMainPersonDeptCode());
        executeHandlePersonRecordService.saveExecuteHandlePersonRecord(dto);
        dto.setHandlePersonName(executeCluHandoutRecordDto.getCluAppointedAssistPersonName());
        dto.setHandleDeptName(executeCluHandoutRecordDto.getCluAppointedAssistPersonDeptName());
        dto.setHandleDeptCode(executeCluHandoutRecordDto.getCluAppointedAssistPersonDeptCode());
        dto.setHandlePersonType(Constant.STRING_2);
        dto.setHandlePersonId(executeCluHandoutRecordDto.getCluAppointedAssistPersonId());
        executeHandlePersonRecordService.saveExecuteHandlePersonRecord(dto);
    }

    /**
     * 描述: 新增裁执分离事件信息
     *
     * @param executeCluHandoutRecordDto
     * @return void
     * @author zhanghongqiang
     * @Date 2022/9/5
     */
    private void saveExecuteEvent(ExecuteCluHandoutRecordDto executeCluHandoutRecordDto) {
        try {
            ExecuteMainInfo executeMainInfo =new ExecuteMainInfo();
            executeMainInfo.setExecuteId(executeCluHandoutRecordDto.getExecuteId());
            ExecuteMainInfo mainInfo = executeMainInfoMapper.selectOne(executeMainInfo);
            ExecuteEventSaveDto dto = new ExecuteEventSaveDto();
            dto.setExecuteId(mainInfo.getExecuteId());
            dto.setExecuteName(mainInfo.getExecuteName());
            evidenceInventoryService.saveExecuteEvent(dto);
        } catch (Exception e) {
            logger.error("新增裁执分离事件失败", e);
        }
    }
}